package com.qingfeng.controller.common;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.qingfeng.constant.enums.ErrorMessages;
import com.qingfeng.framework.bind.DateEditor;
import com.qingfeng.utils.DateUtil;

/**
 * 类BaseController.java的实现描述：controller公用代码
 * 
 * @author wangyugang 2015年7月29日 下午2:18:35
 */
public class BaseController {
	
	private static Logger logger = Logger.getLogger(BaseController.class);
	public String dateFormat = DateUtil.DateMode_4;
	@InitBinder
    private void initBinder(HttpServletRequest request,
                            ServletRequestDataBinder binder) throws Exception{
        binder.registerCustomEditor(Date.class, new DateEditor());  
    }
	/**
	 * 将Java对象转换为JSON对象并响应给浏览器端
	 * 
	 * @param obj
	 * @param response
	 */
	protected void sendJson(Object obj, HttpServletResponse response) {
		try {
		    SerializerFeature[] features={SerializerFeature.DisableCircularReferenceDetect};
			response.getWriter().print(JSON.toJSONStringWithDateFormat(obj,this.dateFormat,features));
			response.getWriter().flush();
			response.getWriter().close();
		} catch (IOException e) {
			logger.error("输入json异常", e);
		}
	}

	/**
	 * 发送一个完成的JSON响应<br>
	 * 
	 * @param response
	 */
	protected void sendJsonSuccess(HttpServletResponse response) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("success", true);
		sendJson(map, response);
	}

	/**
	 * 发送一个完成的JSON响应,同时带有结果对象
	 * 
	 * @param result
	 * @param response
	 */
	protected void sendJsonSuccess(Object result, HttpServletResponse response) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("success", true);
		map.put("result", result);
		sendJson(map, response);
	}

	/**
	 * 发送一个完成的JSON响应,同时带有结果和分页对象
	 * @param result
	 * @param page
	 * @param response
	 */
	protected void sendJsonSuccessPage(Object result, Integer count, HttpServletResponse response) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("success", true);
		map.put("result", result);
		map.put("count", count);
		sendJson(map, response);
	}

	/**
	 * 发送一个失败的JSON响应,同时带有错误信息
	 * 
	 * @param result
	 * @param response
	 */
	protected void sendJsonError(HttpServletResponse response) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("success", false);
		sendJson(map, response);
	}

	/**
	 *  返回一个失败的错误json，同时带有错误信息
	 * @param response
	 */
	protected void sendJsonError(ErrorMessages errorMessages,HttpServletResponse response) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("success", false);
		map.put("errCode", errorMessages.getErrCode());
		map.put("info", errorMessages.getErrInfo());
		sendJson(map, response);
	}
	
	protected void sendJsonSuccess(String info,Object result, HttpServletResponse response) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("success", true);
        map.put("info", info);
        map.put("result", result);
        sendJson(map,response);
    }
}